﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ELab
{
    public static class ThreadHelperClass
    {
        delegate void SetTextCallback(Form f, Control ctrl, string text);
        delegate void AddControlCallback(Form f, Control ctrl, Control newctrl);
        delegate void SetEnableCallback(Form f, Control ctrl, bool isEnable);
        delegate void ClearControlCallback(Form f, Control ctrl, bool isClear);
        delegate void SetBackgroundtCallback(Form f, Control ctrl, Image bgr);

        /// <summary>
        /// Set text property of various controls
        /// </summary>
        /// <param name="form">The calling form</param>
        /// <param name="ctrl"></param>
        /// <param name="text"></param>
        public static void SetText(Form form, Control ctrl, string text)
        {
            // InvokeRequired required compares the thread ID of the 
            // calling thread to the thread ID of the creating thread. 
            // If these threads are different, it returns true. 
            if (ctrl.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                form.Invoke(d, new object[] { form, ctrl, text });
            }
            else
            {
                ctrl.Text = text;
            }
        }

        /// <summary>
        /// Set text property of various controls
        /// </summary>
        /// <param name="form">The calling form</param>
        /// <param name="ctrl"></param>
        /// <param name="text"></param>
        public static void SetBackground(Form form, Control ctrl, Image bgr)
        {
            // InvokeRequired required compares the thread ID of the 
            // calling thread to the thread ID of the creating thread. 
            // If these threads are different, it returns true. 
            if (ctrl.InvokeRequired)
            {
                SetBackgroundtCallback d = new SetBackgroundtCallback(SetBackground);
                form.Invoke(d, new object[] { form, ctrl, bgr });
            }
            else
            {
                ctrl.BackgroundImage = bgr;
                ctrl.BackgroundImageLayout = ImageLayout.Stretch;
            }
        }

        public static void AddControl(Form form, Control ctrl, Control newctrl)
        {
            // InvokeRequired required compares the thread ID of the 
            // calling thread to the thread ID of the creating thread. 
            // If these threads are different, it returns true. 
            if (ctrl.InvokeRequired)
            {
                AddControlCallback d = new AddControlCallback(AddControl);
                form.Invoke(d, new object[] { form, ctrl, newctrl });
            }
            else
            {
                ctrl.Controls.Add(newctrl);
            }
        }
        public static void ClearControl(Form form, Control ctrl, bool isClear)
        {
            // InvokeRequired required compares the thread ID of the 
            // calling thread to the thread ID of the creating thread. 
            // If these threads are different, it returns true. 
            if (ctrl.InvokeRequired)
            {
                ClearControlCallback d = new ClearControlCallback(ClearControl);
                form.Invoke(d, new object[] { form, ctrl, isClear });
            }
            else
            {
                if (!isClear) return;
                ctrl.Controls.Clear();
            }
        }
        public static void SetEnable(Form form, Control ctrl, bool isEnable)
        {
            // InvokeRequired required compares the thread ID of the 
            // calling thread to the thread ID of the creating thread. 
            // If these threads are different, it returns true. 
            if (ctrl.InvokeRequired)
            {
                SetEnableCallback d = new SetEnableCallback(SetEnable);
                form.Invoke(d, new object[] { form, ctrl, isEnable });
            }
            else
            {
                ctrl.Visible = isEnable;
            }
        }
    }
}
